<template>
  <view class="content-wrap">
    <view class="user u-p-l-30 u-flex-col">
      <u-avatar size="120" :src="userInfo.head" class="user-head"></u-avatar>
      <view class="user-info u-p-l-30">
        <view class="user-info-name u-m-t-30 u-m-b-30 t-blue">{{ userInfo.personnelName }}</view>
        <view class="user-info-unit t-white">{{ userInfo.customerShortName }}</view>
      </view>
    </view>
    <view class="u-m-t-60">
      <u-card margin="0 30rpx 40rpx" :border="false" box-shadow="0px 0px 8px #ddd6d6"
        :body-style="{ padding: '30rpx 10rpx' }" :show-head="false" :show-foot="false" @click="changePassword">
        <view slot="body">
          <view class="card-row flex u-p-l-30 u-col-center">
            <text class="t-title">修改密码</text>
          </view>
        </view>
      </u-card>
      <u-card margin="0 30rpx 40rpx" :border="false" box-shadow="0px 0px 8px #ddd6d6"
        :body-style="{ padding: '30rpx 10rpx' }" :show-head="false" :show-foot="false" @click="clear">
        <view slot="body">
          <view class="card-row flex u-row-between u-p-l-30 u-p-r-30 u-col-center">
            <text class="t-title">清除缓存</text>
            <text class="t-t-section">{{ fileSizeString }}</text>
          </view>
        </view>
      </u-card>
      <u-card margin="0 30rpx" :border="false" box-shadow="0px 0px 8px #ddd6d6" :body-style="{ padding: '30rpx 10rpx' }"
        :show-head="false" :show-foot="false">
        <view slot="body">
          <view class="card-row flex u-p-l-30 u-p-r-30 u-col-center u-row-between">
            <text class="t-title">关于</text>
            <view class="u-flex u-col-center">
              <text class="t-section">版本号: {{ version }}</text>
            </view>
          </view>
        </view>
      </u-card>
    </view>
    <u-button type="primary" hover-class="none" @click="UserLogout" :custom-style="signOutBtnStyle"
      class="u-m-t-50">退出登录</u-button>
    <u-tabbar bg-color="#0d1a51" inactive-color="#666" active-color="#fff" height="60px" icon-size="30"
      mid-button-size="40" :list="barList" :mid-button="midButton"></u-tabbar>
    <u-modal v-model="showClear" :content="content" :show-cancel-button="true" :show-title="false"
      :cancel-style="cancelStyle" :confirm-style="confirmStyle" @confirm="confirm" ref="uModal"
      :async-close="true"></u-modal>

    <u-toast ref="uToast" />
  </view>
</template>

<script>
import { BASE_IP, BASE_URL } from "@/utils/ip.js";
const app = getApp();
export default {
  data() {
    return {
      fileSizeString: "", // 缓存总数
      userInfo: {
        head: "",
        personnelName: "", //人员姓名
        customerShortName: "",
        customerName: "",
      },
      version: "",
      signOutBtnStyle: {
        width: "70%",
        borderRadius: "8px",
        color: "#ffffff",
        fontWeight: "500",
        zIndex: "500",
      },
      cancelStyle: {
        background: "#F1F5F3",
        color: "#333333",
      },
      confirmStyle: {
        background: "#5BAC84",
        color: "#333333",
      },
      showClear: false,
      content: "确定清除缓存?",
      barList: [],
    };
  },
  onLoad() {
    this.init();
    this.getUserInfo();
    this.barList = getApp().globalData.list;
  },
  mounted() {
    this.getStorageSize();

    this.getVersionCode();
  },
  onUnload() {
    // 清除监听keyword
    uni.$off("close");
  },
  computed: {
    midButton() {
      return getApp().globalData.tabbarMidButton;
    },
  },
  methods: {
    /**
     * 初始
     */
    init() {
      const config = getApp().globalData.config;
      if (config?.logo) {
        // #ifdef H5
        this.userInfo.head = BASE_IP + BASE_URL + config.logo;
        // #endif
        // #ifndef H5
        this.userInfo.head = BASE_URL + "/" + config.logo;
        // #endif
      }
    },
    getVersionCode() {
      // #ifdef APP-PLUS
      // 获取版本号
      plus.runtime.getProperty(plus.runtime.appid, (inf) => {
        this.version = inf.version;
      });
      // #endif
    },
    changePassword() {
      uni.navigateTo({
        url: "./changePassword/changePassword",
      });
    },
    clear() {
      this.showClear = true;
    },
    async getUserInfo() {
      const res = await this.$post("/mobile/userInfo/getUserInfo");
      if (res && res.body) {
        this.userInfo.personnelName = res.body.personnelName;
        this.userInfo.customerName = res.body.customerName;
        this.userInfo.customerShortName = res.body.customerShortName;
        if (res.body.personPhoto) {
          // #ifdef  H5
          this.userInfo.head = BASE_IP + BASE_URL + "system/archives/preview/thumb/" + res.body.personPhoto;
          // #endif
          // #ifndef H5
          this.userInfo.head = BASE_URL + "/system/archives/preview/thumb/" + res.body.personPhoto;
          // #endif
        }
      }
    },
    confirm() {
      this.clearStorage();
    },
    /**
     * 退出登录
     */
    UserLogout() {
      this.$get("/login?logout").then((data) => {
        app.globalData.token = "";
        app.globalData.userInfo = {};
        app.globalData.userInfoData = {};
        uni.removeStorageSync("HCuname");
        uni.removeStorageSync("HCpassw");
        uni.reLaunch({ url: "/pages/login/index" });
      });
    },
    // 计算缓存
    getStorageSize() {
      // #ifdef APP-PLUS
      let that = this;
      plus.cache.calculate(function (size) {
        let sizeCache = parseInt(size);
        if (sizeCache == 0) {
          that.fileSizeString = "0B";
        } else if (sizeCache < 1024) {
          that.fileSizeString = sizeCache + "B";
        } else if (sizeCache < 1048576) {
          that.fileSizeString = (sizeCache / 1024).toFixed(2) + "K";
        } else if (sizeCache < 1073741824) {
          that.fileSizeString = (sizeCache / 1048576).toFixed(2) + "M";
        } else {
          that.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "G";
        }
      });
      // #endif
    },
    // 清理缓存
    clearStorage() {
      // #ifdef APP-PLUS
      let that = this;
      let os = plus.os.name;
      if (os == "Android") {
        let main = plus.android.runtimeMainActivity();
        let sdRoot = main.getCacheDir();
        let files = plus.android.invoke(sdRoot, "listFiles");
        let len = files.length;
        for (let i = 0; i < len; i++) {
          let filePath = "" + files[i]; // 没有找到合适的方法获取路径，这样写可以转成文件路径
          plus.io.resolveLocalFileSystemURL(
            filePath,
            function (entry) {
              if (entry.isDirectory) {
                entry.removeRecursively(
                  function (entry) {
                    //递归删除其下的所有文件及子目录
                    uni.showToast({
                      title: "缓存清理完成",
                      duration: 2000,
                    });
                    that.showClear = false;
                    that.getStorageSize(); // 重新计算缓存
                  },
                  function (e) {}
                );
              } else {
                entry.remove();
              }
            },
            function (e) {}
          );
        }
      } else {
        // ios
        plus.cache.clear(function () {
          uni.showToast({
            title: "缓存清理完成",
            duration: 2000,
          });
          that.showClear = false;
          that.getStorageSize();
        });
      }
      // #endif
    },
  },
};
</script>
<style lang="scss" scoped>
.content-wrap{
  background-color: #0d1a51;
  background: url('/static/image/bg.png');
  background-repeat: no-repeat;
  background-size: contain;
}
.user {
  height: 400rpx;
  padding-top: 100rpx;
  &-info {
    &-name {
      letter-spacing: 5rpx;
      font-weight: 500;
    }
  }
}
.red-circle {
  width: 30rpx;
  height: 30rpx;
  border-radius: 50%;
  background: red;
}
</style>
